<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 * 测试controller 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id: config.php 29 2009-03-10 14:30:18Z wph $
 */
pf_load::init('admin_class_admin');
class sysmenu_action extends pf_controller{
	protected $menu_obj = null;

	public function __construct()
	{
		parent::__construct();
		$this->menu_obj = pf_load::init('admin_model_sysmenu');
	}

	public function index()
	{
		$sysmenu_list = $this->get_menu_list();
		$this->assign('sysmenu_list', $sysmenu_list);
		$this->view('admin_admin_view_sysmenu.list');
	}


	public function add()
	{
		$opt = pf_request::get('opt', 'list');
		switch ($opt)
		{
			case 'insert':
				$this->insert();
				break;
			case 'update':
				$this->update();
				break;
			default:
				$this->show_add();
				break;
		}
		
	}

	public function show_add()
	{
		$id = (int)pf_request::get('id', 0);
		$opt = "insert";
		$menu = array();
		if ($id !== 0)
		{
			$menu = $this->menu_obj->get_menu_by_id($id);
			if (false === $menu)
			{
				$menu = array();
			}
			$opt = 'update';
		}
		$sysmenu_list = $this->get_menu_list();
		$this->assign('sysmenu_list', $sysmenu_list);
		$this->assign('opt', $opt);
		$this->assign('menu', $menu);
		$this->view('admin_admin_view_sysmenu.add');
	}

	private function get_menu_list()
	{
		$menu_list = $this->menu_obj->get_sysmenu_list();
		foreach ($menu_list as $key=>$val)
		{
			$level = $val['level'];
			$str = '';
			if ($level > 1)
			{
				$str = "└";
				for($i=0; $i < $level; $i++)
				{
					$str .= '--';
				}
			}
			$menu_list[$key]['cname'] = $str . $menu_list[$key]['cname'];
		}
		return $menu_list;
	}
	
	public function update()
	{
		$data['parentid'] = (int)pf_request::post('parentid', 0);
		$data['cname'] = pf_request::post('cname', '');
		$data['ename'] = pf_request::post('ename', '');
		$data['model'] = pf_request::post('model', '');
		$data['controller'] = pf_request::post('controller', '');
		$data['action'] = pf_request::post('action', '');
		$data['params'] = pf_request::post('params', '');
		$data['display'] = pf_request::post('display', 0);
		$time = time();
		$data['updatetime'] = $time;
		$id = pf_request::post('id', 0);
		$argv = array(
			'data'=> $data,
			'where' => 'id=' . (int)$id,
		);

		$res = $this->menu_obj->update($argv);
		if (false === $res)
		{
			echo "fail";
		} else {
			echo "success";
		}
	}

	public function insert()
	{
		$data['parentid'] = (int)pf_request::post('parentid', 0);
		$data['cname'] = pf_request::post('cname', '');
		$data['ename'] = pf_request::post('ename', '');
		$data['model'] = pf_request::post('model', '');
		$data['controller'] = pf_request::post('controller', '');
		$data['action'] = pf_request::post('action', '');
		$data['params'] = pf_request::post('params', '');
		$data['display'] = pf_request::post('display', 0);

		$time = time();
		$data['updatetime'] = $data['createtime'] = $time;
		$res = $this->menu_obj->insert($data);
		if (false === $res)
		{
			echo "fail";
		} else {
			echo "success";
		}
	}
}
